package com.hancher.plugin.hrlog.burypoint;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 作者 : Hancher ytu_shaoweijie@163.com <br/>
 * 时间 : 2023/6/26 10:01 <br/>
 * 描述 : 埋点点击事件工具类
 */
public class BuryPointUtil {

    /**
     * 修改监听
     *
     * @param activity
     */
    public static void changeListener(Activity activity) {
        if (activity == null) {
            return;
        }
        ViewGroup rootView = activity.findViewById(android.R.id.content);

        //ViewGroup rootView = (ViewGroup) activity.getWindow().getDecorView();
        try {
            setViewProxy(rootView);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener = () -> {
            try {
                setViewProxy(rootView);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        };
        rootView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
        try {
            setViewProxy(rootView);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    private static void setViewProxy(ViewGroup viewGroup) throws IllegalAccessException, InvocationTargetException {
        int count = viewGroup.getChildCount();
        for (int i = 0; i < count; i++) {
            hook(viewGroup.getChildAt(i));
            if (viewGroup.getChildAt(i) instanceof ViewGroup) {
                setViewProxy((ViewGroup) viewGroup.getChildAt(i));
            }
        }
    }

    private static void hook(View view) throws IllegalAccessException, InvocationTargetException {

        try {
            Method getListenerInfo = View.class.getDeclaredMethod("getListenerInfo");
            getListenerInfo.setAccessible(true);
            Object listenereInfo = getListenerInfo.invoke(view);
            try {
                Class<?> listenerInfoClazz = Class.forName("android.view.View$ListenerInfo");
                try {
                    Field mOnClickListener = listenerInfoClazz.getDeclaredField("mOnClickListener");
                    mOnClickListener.setAccessible(true);
                    View.OnClickListener originOnClickListener = (View.OnClickListener) mOnClickListener.get(listenereInfo);
                    if (originOnClickListener == null || originOnClickListener instanceof MyWrapperOnClickListenerer) {
                        return;
                    } else {
                        MyWrapperOnClickListenerer proxyOnClick = new MyWrapperOnClickListenerer(originOnClickListener);
                        mOnClickListener.set(listenereInfo, proxyOnClick);
                    }

                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                }

            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }


        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

    }

}
